<?php

foreach($result AS $r)
  {
    $mid = $r['m']['id'];
    $line = array('MAC and CUSTOMER DETAILS', '', '', '', '', '', '');
	$csv->addRow($line);
    $line = array('', 'MAC_ADDRESS', 'MODEMTYPE', 'STATUSNAME', 'SERVICENAME', 'RUNID', 'ACCOUNTNUMBER');
	$csv->addRow($line);    
    $line = array('', $r['m']['mac'], $r['mt']['description'], $r['s']['statusname'], $r['mst']['name'], $r['rhm']['run_id'], $r['c']['account_number']);
    $csv->addRow($line);
	$line = array('PART REQUEST DETAILS', '', '', '', '', '', '');
	$csv->addRow($line);
	$line = array('', 'DETAILNUMBER', 'XTO', 'XFROM', 'XTODATE', 'T&T', 'NOTES');
	$csv->addRow($line);
	$result2 = mysql_query("SELECT detailnumber, xto, xfrom, xtodate, trackingnumber, notes  FROM partrequests WHERE mac_id = '$mid'");
	IF(mysql_num_rows($result2) == 0) { $line = array('', 'none', '', '', '', '', ''); $csv->addRow($line); }
	ELSE {
	  WHILE($row2 = mysql_fetch_array($result2))
	    {
	      $line = array('', $row2['detailnumber'], $row2['xto'], $row2['xfrom'], $row2['xtodate'], $row2['trackingnumber'], $row2['notes']);
		  $csv->addRow($line);
	    }
	}
	$line = array('POND CASE DETAILS', '', '', '', '', '', '');
	$csv->addRow($line);
	$line = array('', 'CASEID', 'CASESTATUS', 'CREATEDATE', 'PROCESSDATE', '', '');  
	$csv->addRow($line);
	$result3 = mysql_query("SELECT caseid, createdate, processdate, statusname FROM pondcases pc, partrequests pr, statuses s WHERE pc.partrequest_id = pr.id AND pr.mac_id = '$mid' AND pc.status_id = s.id");
	IF(mysql_num_rows($result3) == 0) { $line = array('', 'none', '', '', '', '', ''); $csv->addRow($line); }
	ELSE {
	  WHILE($row3 = mysql_fetch_array($result3))
	    {
	      $line = array('', $row3['caseid'], $row3['statusname'], $row3['createdate'], $row3['processdate'], '', '');
		  $csv->addRow($line);
	    }
	}  
	$line = array('', '', '', '', '', '', '');
	$csv->addRow($line);
	$csv->addRow($line); 
	$csv->addRow($line); 
  }

$datum = date("d.m.Y");
$end = ".csv";
$title = "Export_Cancelled_Swaps_".$datum.".csv";
echo $csv->render($title);
?>